﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace GMS.Client.Utility
{
    /// <summary>
    /// Xml辅助类
    /// </summary>
    public class XmlHelper
    {
        private XDocument Root { get; set; }
        private string FilePath { get; set; }

        public XmlHelper(string filePath)
        {
            this.Root = XDocument.Load(filePath);
            this.FilePath = filePath;
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="isNode"></param>
        /// <returns></returns>
        public T GetValue<T>(string name, bool isNode = true)
        {
            return isNode ? GetNodeValue<T>(name) : GetAttributeValue<T>(name);
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="isNode"></param>
        public void SetValue(string name, object value, bool isNode = true)
        {
            if (isNode)
            {
                SetNodeValue(name, value);
            }
            else
            {
                SetAttributeValue(name, value);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            Root.Save(FilePath);
        }

        /// <summary>
        /// 根据节点名称获取节点值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        private T GetNodeValue<T>(string name)
        {
            var t = default(T);
            var nodes = from query in Root.Descendants(name) select query;
            foreach (var node in nodes)
            {
                t = (T)Convert.ChangeType(node.Value, typeof(T));
                break;
            }
            return t;
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        private T GetAttributeValue<T>(string attributeName)
        {
            var t = default(T);
            var attributes = from query in Root.Descendants().Attributes(attributeName) select query;
            foreach (var attribute in attributes)
            {
                t = (T)Convert.ChangeType(attribute.Value, typeof(T));
                break;
            }
            return t;
        }

        /// <summary>
        /// 设置节点值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        private void SetNodeValue(string name, object value)
        {
            var nodes = from query in Root.Descendants(name) select query;
            foreach (var node in nodes)
            {
                node.Value = value.ToString();
            }
        }

        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="attributeValue"></param>
        private void SetAttributeValue(string attributeName, object attributeValue)
        {
            var attributes = from query in Root.Descendants().Attributes(attributeName) select query;
            foreach (var attribute in attributes)
            {
                attribute.Value = attributeValue.ToString();
            }
        }
    }
}
